﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;

namespace BUS
{
   public class LoaiBenhBus
    {
        public string connect = QuiDinhBus.connect;
        public static QLPMDataContext db;
        // db = new QLPMDataContext(connect);
        public IEnumerable<LoaiBenh> GetAll()
        {
            IEnumerable<LoaiBenh> result = null;
            try
            {
                db = new QLPMDataContext(connect);
                result = from t in db.LoaiBenhs select t;
                //db.Dispose();

            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }
        public int Insert(LoaiBenh info)
        {
            int id = -1;
            try
            {
                 db = new QLPMDataContext(connect);
                db.LoaiBenhs.InsertOnSubmit(info);
                db.SubmitChanges();

                id = info.ID;
            }
            catch (Exception)
            {

                throw;
            }

            return id;
        }
        public LoaiBenh Get(int key)
        {
            LoaiBenh info = new LoaiBenh();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.LoaiBenhs where qd.ID == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }
        public bool Delele(int key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);

                LoaiBenh del = (from t in db.LoaiBenhs where t.ID == key select t).SingleOrDefault();

                db.LoaiBenhs.DeleteOnSubmit(del);

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public bool Update(LoaiBenh info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from qd in db.LoaiBenhs where qd.ID == info.ID select qd).SingleOrDefault();
                t.TenLoaiBenh = info.TenLoaiBenh;
                t.UserModify = info.UserModify;
                t.ModifyDate = info.ModifyDate;
                db.SubmitChanges();


                flag = true;

            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public void CheckInsert()
        {

            try
            {
                int count = 0;
                db = new QLPMDataContext(connect);
                count = (from t in db.LoaiBenhs select t).Count();
                //db.Dispose();
                QuiDinh qd = (from t in db.QuiDinhs where t.QD == "SoLoaiBenh" select t).SingleOrDefault();
                if (count > qd.Value)
                {
                    throw new System.ArgumentException("So loai benh khong vuot qua :", qd.Value.ToString());
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
